<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="js/jquery-3.2.1.min.js"></script>
    <style>
        .top{
            width: 400px;
            height: 400px;
            background-color: pink;
            margin: 0 auto;
            position: relative;
            border: 5px solid rgb(188, 228, 252);
        }
        .first{
            width: 40px;
            height: 40px;
            background-color: rgb(231, 233, 127);
            position: absolute;
            top: 50px;
            left: 50px;
            border: 5px solid white;
        }
        .main{
            width: 100%;
            height: 1000px;
        }
        .div1{
            width: 1000px;
            height: 800px;
            background-color: blueviolet;
            margin: 0 auto;
            margin-top: 50px;
            position: relative;
        }
        .ret{
            right: 100px;
            bottom: 120px;
            width: 40px;
            height: 40px;
            position: fixed;
        }
        .fixed{
            right: 100px;
            bottom: 120px;
            width: 40px;
            height: 40px;
            background-color:red;
        }
    </style>
</head>
<body>
    <div class="top" id="top">
        <div class="first" id=first></div>
    </div>
    <div class="main" id="main">
        <div class="div1" id="div1"></div>
        <div class="ret" id="ret"></div>
    </div>
    
    <script>
        $(function(){
            $("#first").mousedown(function(e){
                var x=e.clientX-$("#first").position().left;
                var y=e.clientY-$("#first").position().top;
                $(document).mousemove(function(e){
                    var left=e.clientX-x;
                    var right=e.clientY-y;
                    var max=$("#top").width()-$("#first").outerWidth();
                    var max1=$("#top").height()-$("#first").outerHeight();
                    if(left>max){
                        left=max;
                    }else if(left<0){
                        left=0;
                    }
                    if(right>max1){
                        right=max1;
                    }else if(right<0){
                        right=0;
                    }
                    $("#first").css("left",left+"px");
                    $("#first").css("top",right+"px");
                })
                $(document).mouseup(function(){
                    $(this).unbind()
                })

            })

            var height =$("#top").outerHeight();
            $(window).scroll(function(){
                if($(window).scrollTop()>height){
                    $("#ret").show();
                    $("#ret").css("background-color","red")
                }else{
                    $("#ret").fadeOut();
                }
            })
            $("#ret").click(function(){
                $("body,html").stop().animate({
                    scrollTop:0
                },1000)
            })
        })
    </script>
</body>
</html>